package org.ceteca.explica.client.partners;

import java.util.HashSet;

import org.ceteca.explica.client.gui.ExplicaDetailEntityView;
import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.MultiSelectionSubentityOT;
import org.ceteca.explica.core.common.SelectionSubentityOT;
import org.ceteca.explica.core.partners.ContactOT;
import org.ceteca.explica.core.partners.InterfacePartners;
import org.ceteca.explica.core.partners.RappelIntervalOT;
import org.ceteca.explica.core.partners.RappelTypeOT;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.explica.core.util.OperationResult;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;


/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class RappelTypeClient extends CommonClient {

	private Text entryRappelTypeId, entryRappelTypeDescription, entryVersion;

	private Composite panelLista;

	
	
	
	public RappelTypeClient() {
		super(	RappelTypeOT.class,
				InterfacePartners.EXPLICA_EDIT_RAPPELTYPE_GLADE_PATH,
				InterfacePartners.EDIT_RAPPELTYPE_DIALOG_NAME);

	}

	public void getWidgets() {

		boolean allOk = true;

		if ((entryRappelTypeId = ((Text) this.getWidget(
				InterfacePartners.EDIT_RAPPELTYPE_CODE_WIDGETNAME))) == null) {
			allOk = false;
			this.logger.error("Error getting entry Rappel Type Widget:"
					+ InterfacePartners.EDIT_RAPPELTYPE_CODE_WIDGETNAME);

		}

		if ((entryRappelTypeDescription = ((Text) this.getWidget(
				InterfacePartners.EDIT_RAPPELTYPE_NAME_WIDGETNAME))) == null) {
			allOk = false;
			this.logger.error("Error getting entry Rappel Type Widget:"
					+ InterfacePartners.EDIT_RAPPELTYPE_NAME_WIDGETNAME);
		}

		if ((entryVersion = ((Text) this
				.getWidget(
						InterfacePartners.EDIT_VERSION_WIDGETNAME))) == null) {
			allOk = false;
			this.logger
					.error("Error getting entry Version Widget:"
							+ InterfacePartners.EDIT_VERSION_WIDGETNAME);

		}
		
		if ((panelLista = (Composite) this
				.getWidget(
						InterfacePartners.EDIT_RAPPELTYPE_RAPPELINTERVAL_PANEL_WIDGETNAME)) == null) {
			allOk = false;
			this.logger
					.error("Error getting entry Rappel Type Widget:"
							+ InterfacePartners.EDIT_RAPPELTYPE_RAPPELINTERVAL_PANEL_WIDGETNAME);
		}

		if (!allOk)
			System.exit(-1);

	}

	public void setWidgetsValues(BaseVO entity) {

		RappelTypeOT ent = (RappelTypeOT) entity;
		getWidgets();

		entryRappelTypeId.setText(Converter.getInstance().toString(
				ent.getIdRappelType()));
		entryRappelTypeDescription.setText(ent.getDescription() == null ? ""
				: ent.getDescription());

		entryVersion.setText(Converter.getInstance().toString(((RappelTypeOT)entity).getVersion()));		
		
		ExplicaDetailEntityView view = this.entEditView.getMultiSelectEntityView(InterfacePartners.EXPLICA_ENTITY_RAPPELINTERVAL_ID);
		if (view != null) {
			view.setRegistries(ent.getRappelIntervals());
		}else
		{
			this.logger.error("Error. Rappel Interval Detail view");
			System.exit(-1);
		}

	}

	
	
	
	
	@Override
	public BaseVO getEntityData() {
		RappelTypeOT entity = new RappelTypeOT();
		if (this != null) {
			entity
					.setDescription(((Text) this
							.getWidget(InterfacePartners.EDIT_RAPPELTYPE_NAME_WIDGETNAME))
							.getText());
			if (entity.getRappelIntervals() == null)
				entity.setRappelIntervals(new HashSet<RappelIntervalOT>());
			if (!isDetail()) {
				String subentityId = InterfacePartners.EXPLICA_ENTITY_RAPPELINTERVAL_ID;
				entity.getRappelIntervals().clear();
				
				ExplicaDetailEntityView view = this.entEditView.getMultiSelectEntityView(subentityId);
				entity.getRappelIntervals().addAll(view.getRegistries());
					

			}

			if (selectedEntity != null) {
				entity.setVersion(((RappelTypeOT) this.selectedEntity)
						.getVersion());
				entity.setIdRappelType(((RappelTypeOT) this.selectedEntity)
						.getIdRappelType());
			}
		}
		return entity;
	}

	@Override
	public OperationResult validateEntity() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setEditionMode(boolean enabled) {
		getWidgets();
		entryRappelTypeDescription.setEnabled(enabled);
		entryRappelTypeId.setEnabled(false);
		entryVersion.setEnabled(false);
		
		if (entryRappelTypeId.getText().equals("0"))
			panelLista.setEnabled(false);
		else
			panelLista.setEnabled(enabled);
	}

	@Override
	public void buildSelectionSets() {
		lSelectionSubentities = new HashSet<SelectionSubentityOT>();

		lMultiSelectionSubentities = new HashSet<MultiSelectionSubentityOT>();

		lMultiSelectionSubentities.add(new MultiSelectionSubentityOT(
				InterfacePartners.EXPLICA_ENTITY_RAPPELINTERVAL_ID,
				"treeRappelTypeRappelIntervals", this, this.getSwtWidget(InterfacePartners.EDIT_RAPPELTYPE_RAPPELINTERVAL_PANEL_WIDGETNAME), "rappelIntervals", "rappelType"));

	}

	@Override
	public void init_specific_components() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void build_extension_fields_view() {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void fillEntityData() {
		if (this.selectedEntity != null && this.entEditView != null) {
			getWidgets();
			setWidgetsValues((RappelTypeOT) selectedEntity);
		}
	}
}
